package com.geniusk.system.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.geniusk.common.core.JSONResult;
import com.geniusk.system.model.bo.menu.DeleteMenuByIdsBO;
import com.geniusk.system.model.bo.menu.InsertMenuBO;
import com.geniusk.system.model.bo.menu.SelectMenuBO;
import com.geniusk.system.model.bo.menu.UpdateMenuBO;
import com.geniusk.system.model.pojo.Menu;
import com.geniusk.security.annotation.PreFuncAuth;
import com.geniusk.security.core.LoginUser;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理接口
 */
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询菜单
     */
    @PostMapping("/selectAllMenu")
    @PreFuncAuth("SYS:MENU:SELECT")
    public JSONResult selectAllMenu(SelectMenuBO selectMenuBO) {
        List<Menu> list;
        LoginUser loginUser = SecurityUtil.getLoginUser();
        if(loginUser.getIsRoot()){
            Menu menu = JSONUtil.parse(selectMenuBO).toBean(Menu.class);
            list = menuService.selectAllMenu(menu);
        }else{
            HashMap<String,Object> hashMap = new HashMap<>();
            hashMap.put("userId",loginUser.getUserId());
            hashMap.put("menuName",selectMenuBO.getMenuName());
            hashMap.put("status",selectMenuBO.getStatus());
            list = menuService.selectMenuByUserId(hashMap);
        }
        return JSONResult.ok(list);
    }

    /**
     * 添加菜单
     */
    @PostMapping("/insertMenu")
    @PreFuncAuth("SYS:MENU:INSERT")
    public JSONResult insertMenu(@Valid @RequestBody InsertMenuBO insertMenuBO) {
        Menu menu = JSONUtil.parse(insertMenuBO).toBean(Menu.class);
        Long menuId = IdUtil.getSnowflake().nextId();
        menu.setMenuId(menuId);
        menu.setCreateBy(SecurityUtil.getUsername());
        int rows = menuService.insertMenu(menu);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 修改菜单
     */
    @PostMapping("/updateMenu")
    @PreFuncAuth("SYS:MENU:UPDATE")
    public JSONResult updateMenu(@Valid @RequestBody UpdateMenuBO updateMenuBO) {
        Menu menu = JSONUtil.parse(updateMenuBO).toBean(Menu.class);
        menu.setUpdateBy(SecurityUtil.getUsername());
        int rows = menuService.updateMenu(menu);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 删除菜单
     */
    @PostMapping("/deleteMenuByIds")
    @PreFuncAuth("SYS:MENU:DELETE")
    public JSONResult deleteMenuByIds(@Valid @RequestBody DeleteMenuByIdsBO deleteMenuByIdsBO) {
        int rows = menuService.deleteMenuByIds(deleteMenuByIdsBO.getMenuIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
